import pygame
from game import Game


class Player1(pygame.sprite.Sprite):
    def __init__(self, left: int, top: int, speed: int):
        super(Player1, self).__init__()

        self.image = pygame.image.load(f"img/player1/人物右.png")
        self.image2 = pygame.image.load(f"img/player1/人物左.png")
        self.image3 = pygame.image.load(f"img/player1/人物右.png")
        self.images_left = [pygame.image.load(f"img/player1/走左边/frame-{i}.png") for i in range(1, 5)]
        self.images_right = [pygame.image.load(f"img/player1/走右边/frame-{i}.png") for i in range(1, 5)]

        self.character_images = {
            "right": pygame.image.load("img/player1/人物右.png"),
            "left": pygame.image.load("img/player1/人物左.png"),
            "jump_right": pygame.image.load("img/player1/人物跳右.png"),
            "jump_left": pygame.image.load("img/player1/人物跳左.png"),
        }
        self.player_image_left = pygame.transform.scale(self.character_images["left"], (40, 60))
        self.player_image_right = pygame.transform.scale(self.character_images["right"], (40, 60))
        self.player_image_jump_right = pygame.transform.scale(self.character_images["jump_right"], (40, 60))
        self.player_image_jump_left = pygame.transform.scale(self.character_images["jump_left"], (40, 60))

        self.rect = self.images_left[0].get_rect()
        self.rect.left = left
        self.rect.top = top

        self.player_old_left = 0
        self.player_old_top = 0
        self.player_speed = speed

        Game.direction = "right"
        Game.current_images = self.images_right

        self.renwu_font = pygame.font.Font("Font/font.ttf", 20)
        self.score_Player = self.renwu_font.render("弓箭", True, (200, 150, 0))

    def update(self, *args):
        Game.window.blit(self.score_Player, (self.rect.x + 8, self.rect.y - 30))
        if Game.direction_W:
            self.rect.top -= self.player_speed
            if self.image == self.player_image_left:
                self.image = self.player_image_jump_left
            elif self.image == self.player_image_right:
                self.image = self.player_image_jump_right
        if Game.direction_D and not Game.direction_W:
            Game.current_images = self.images_right
            self.image = Game.current_images[args[0] % len(Game.current_images)]

        if Game.direction_A and not Game.direction_W:
            Game.current_images = self.images_left
            self.image = Game.current_images[args[0] % len(Game.current_images)]

    def left_right(self):
        self.player_old_top = self.rect.top
        self.player_old_left = self.rect.left
        if Game.direction_D:
            Game.direction = "right"
            self.image = self.image3
            self.rect.left += self.player_speed
        elif Game.direction_A:
            Game.direction = "left"
            self.image = self.image2
            self.rect.left -= self.player_speed
        if Game.direction_W and Game.direction_D:
            self.image = self.player_image_jump_right
        elif Game.direction_W and Game.direction_A:
            self.image = self.player_image_jump_left

        else:
            if self.image == self.player_image_jump_right:
                self.image = self.player_image_right
            elif self.image == self.player_image_jump_left:
                self.image = self.player_image_left

    def confine(self):
        self.player_old_top = self.rect.top
        self.player_old_left = self.rect.left
        if self.rect.x <= 0:
            self.rect.x = 0
        if self.rect.x >= Game.W - self.rect.width:
            self.rect.x = Game.W - self.rect.width
        if self.rect.y <= 0:
            self.rect.y = 0
        if self.rect.y >= Game.H - self.rect.height:
            self.rect.y = Game.H - self.rect.height
            Game.G_speed = 0
            Game.direction_W = False

    def hit_wall(self):
        for row in range(Game.MAP_HEIGHT):
            for col in range(Game.MAP_WIDTH):
                if Game.wall_list[row][col] in (1, 2, 4):
                    wall_rect = pygame.Rect(col * Game.BLOCK_SIZE, row * Game.BLOCK_SIZE, Game.BLOCK_SIZE,
                                            Game.BLOCK_SIZE)

                    if self.rect.colliderect(wall_rect):
                        if self.rect.left < wall_rect.left:
                            self.rect.left = self.player_old_left

                        elif self.rect.right > wall_rect.right:
                            self.rect.left = self.player_old_left

                    if self.rect.colliderect(wall_rect):
                        if self.rect.top <= wall_rect.bottom:
                            self.rect.top = wall_rect.bottom
